<?php 
/*
 module:		价格管理
 create_time:	2021-09-24 18:27:24
 author:		
 contact:		
*/

namespace app\admin\controller\Customer;

use app\admin\service\Customer\MoneyService;
use app\admin\model\Customer\Money as MoneyModel;
use app\admin\controller\Admin;
use think\facade\Db;

class Money extends Admin {


	/*首页数据列表*/
	function index(){
		if (!$this->request->isAjax()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$where = [];
			$where['state'] = $this->request->param('state', '', 'serach_in');

			$order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc

			$field = 'cm_id,original_price,price,state,minute,sort';
			$orderby = ($sort && $order) ? $sort.' '.$order : 'sort desc,cm_id desc';

			$res = MoneyService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
			return json($res);
		}
	}

	/*修改排序开关按钮操作*/
	function updateExt(){
		$postField = 'cm_id,state,sort';
		$data = $this->request->only(explode(',',$postField),'post',null);
		if(!$data['cm_id']) $this->error('参数错误');
		try{
			MoneyModel::update($data);
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*添加*/
	function add(){
		if (!$this->request->isPost()){
			return view('add');
		}else{
			$postField = 'original_price,price,state,minute,sort';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = MoneyService::add($data);
			if($res && empty($data['sort'])){
				MoneyModel::update(['sort'=>$res,'cm_id'=>$res]);
			}
			return json(['status'=>'00','msg'=>'添加成功']);
		}
	}

	/*修改*/
	function edit(){
		if (!$this->request->isPost()){
			$cm_id = $this->request->get('cm_id','','serach_in');
			if(!$cm_id) $this->error('参数错误');
			$this->view->assign('info',checkData(MoneyModel::find($cm_id)));
			return view('edit');
		}else{
			$postField = 'cm_id,original_price,price,state,minute,sort';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = MoneyService::edit($data);
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}

	/*删除*/
	function del(){
		$idx =  $this->request->post('cm_id', '', 'serach_in');
		if(!$idx) $this->error('参数错误');
		try{
			MoneyModel::destroy(['cm_id'=>explode(',',$idx)],true);
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*箭头排序*/
	function arrowsort(){
		$postField = 'cm_id,sortid,type';
		$data = $this->request->only(explode(',',$postField),'post',null);
		if(empty($data['sortid'])){
			$this->error('操作失败，当前数据没有排序号');
		}
		if($data['type'] == 1){
			$where['sort'] = ['>',$data['sortid']];
			$info = MoneyModel::where(formatWhere($where))->order('sort asc')->find();
		}else{
			$where['sort'] = ['<',$data['sortid']];
			$info = MoneyModel::where(formatWhere($where))->order('sort desc')->find();
		}
		if(empty($info['sort'])){
			$this->error('操作失败，目标位置没有排序号');
		}
		if($info){
			try{
				MoneyModel::update(['cm_id'=>$data['cm_id'],'sort'=>$info['sort']]);
				MoneyModel::update(['cm_id'=>$info['cm_id'],'sort'=>$data['sortid']]);
			}catch(\Exception $e){
				throw new \think\exception\ValidateException ($e->getMessage());
			}
		}else{
			$this->error('目标位置没有数据');
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}



}

